<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Models\WxTenant;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;

trait formGroupPushTrait{
    public function index_group_push(Content $content)
    {
        return $content->title('群推送配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_group_push()->render()}</div>
HTML
            );
    }
    protected function form_group_push(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">群推送教程：<a href="https://doc.minisns.cn/project-13/" target="_blank">https://doc.minisns.cn/project-13/</a></div>';
        }, '');

        $form->number('group_push_min_post_id', '笔记最小推送id')->help('如果过于旧的笔记不想被推送，则可以配置这个，小于这个id的笔记都不会被推送')->default(Settings::get('group_push_min_post_id', 0));
        $form->number('group_push_min_used_id', '闲置最小推送id')->help('如果过于旧的闲置不想被推送，则可以配置这个，小于这个id的闲置都不会被推送')->default(Settings::get('group_push_min_used_id', 0));

        $form->divider();
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">输入uid，那么对应的分站推送到群时，会自动带上该用户的推广标识</div>';
        }, '');
        $tenants = WxTenant::all();
        if($tenants){
            foreach ($tenants as $tenant){
                if($tenant->tenant_id > 0){
                    $form->text('tenant_'.$tenant->tenant_id.'_push_with_uid', $tenant->name)->default(Settings::get('tenant_'.$tenant->tenant_id.'_push_with_uid', 0));
                }
            }
        }

        $form->action('settings/action/group_push');
        return $form;
    }

    public function group_push(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('group_push_min_post_id', trim($request->input('group_push_min_post_id')), true);
        Settings::set('group_push_min_used_id', trim($request->input('group_push_min_used_id')), true);

        $tenants = WxTenant::all();
        if($tenants){
            $env_updates = [];
            foreach ($tenants as $tenant){
                if($tenant->tenant_id > 0){
                    Settings::set('tenant_'.$tenant->tenant_id.'_push_with_uid', $request->input('tenant_'.$tenant->tenant_id.'_push_with_uid'), true);
                    $env_updates['TENANT_'.$tenant->tenant_id.'_PUSH_WITH_UID'] = (int)(trim_all_space($request->input('tenant_'.$tenant->tenant_id.'_push_with_uid')));
                }
            }
            _update_env($env_updates, [], 1);
        }

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);

        return $this->response()->success('成功！');
    }

}
